package com.qiantu.api.db;

import android.content.Context;
import android.text.TextUtils;

import com.qiantu.api.db.database.LocalDataBase;
import com.qiantu.api.db.greendao.DeviceCustomKeyBeanDao;
import com.qiantu.api.entity.DeviceCustomKeyBean;

import java.util.List;

public class LLDeviceCustomDBManager {
    private final String TAG = "LLDeviceCustomDBManager";
    private volatile static LLDeviceCustomDBManager instance = null;
    private final static Object object = new Object();
    private final DeviceCustomKeyBeanDao mDeviceCustomKeyBeanDao;
    private Context context;

    private LLDeviceCustomDBManager(Context context) {
        this.context = context;
        mDeviceCustomKeyBeanDao = LocalDataBase.getInstance().getDaoSession(context).getDeviceCustomKeyBeanDao();
    }

    public static LLDeviceCustomDBManager getInstance(Context context) {
        if (instance == null) {
            synchronized (object) {
                if (instance == null) {
                    instance = new LLDeviceCustomDBManager(context.getApplicationContext());
                }
            }
        }
        return instance;
    }

    public void clearFromDB() {
        mDeviceCustomKeyBeanDao.deleteAll();
    }

    /**
     * 从服务器更新设备数据
     *
     * @param deviceCustomKeyBeans
     */
    public void refreshAllCustomsTx(List<DeviceCustomKeyBean> deviceCustomKeyBeans, HandleListener listener) {
        LocalDataBase.getInstance().getDaoSession(context).runInTx(() -> {
            clearFromDB();
            if(deviceCustomKeyBeans == null){
                return;
            }
            for (DeviceCustomKeyBean deviceCustomKeyBean : deviceCustomKeyBeans) {
                mDeviceCustomKeyBeanDao.save(deviceCustomKeyBean);
            }
        });
    }
    /**
     * 保存或者更新群组设备
     *
     * @param deviceCustomKeyBeans
     */
    public void addBatch(List<DeviceCustomKeyBean> deviceCustomKeyBeans) {
        LocalDataBase.getInstance().getDaoSession(context).runInTx(() -> {
            for (DeviceCustomKeyBean deviceCustomKeyBean : deviceCustomKeyBeans) {
//                mSourceHomepageBeanDao.save(SourceHomepageBean);
            }
        });
    }

    public void put(DeviceCustomKeyBean deviceCustomKeyBean) {
        if (deviceCustomKeyBean == null)
            return;
        String serialNo = deviceCustomKeyBean.getDeviceSerialNo();
        if (serialNo != null && !TextUtils.isEmpty(serialNo)) {
            DeviceCustomKeyBean oldDevice = mDeviceCustomKeyBeanDao.queryBuilder()
                    .where(DeviceCustomKeyBeanDao.Properties.DeviceSerialNo.eq(serialNo))
                    .where(DeviceCustomKeyBeanDao.Properties.CustomKeySerialNo.eq(deviceCustomKeyBean.getCustomKeySerialNo()))
                    .unique();
            if (oldDevice != null) {
                deviceCustomKeyBean.setLocalId(oldDevice.getLocalId());
            }
        }
        mDeviceCustomKeyBeanDao.save(deviceCustomKeyBean);
    }

    public List<DeviceCustomKeyBean> getDataByDeviceSerialNo(String deviceSerialNo) {
        return mDeviceCustomKeyBeanDao.queryBuilder()
                .where(DeviceCustomKeyBeanDao.Properties.DeviceSerialNo.in(deviceSerialNo))
                .list();
    }

//    public void updateDeviceCustomKeys(String deviceSerialNo, List<DeviceCustomKeyBean> customKeyList){
//        if (!TextUtils.isEmpty(deviceSerialNo)) {
//            DeviceBean device = mDeviceBeanDao.queryBuilder()
//                    .where(DeviceBeanDao.Properties.DeviceSerialNo.eq(deviceSerialNo))
//                    .unique();
//            if (device != null) {
//                device.setDeviceCustomKeys(customKeyList);
//                mDeviceBeanDao.save(device);
//            }
//        }
//    }

//    public void changeIsHomepage(String serialNo,boolean isHomepage) {
//        if (!TextUtils.isEmpty(serialNo)) {
//            SourceHomepageBean homepage = mSourceHomepageBeanDao.queryBuilder()
//                    .where(SourceHomepageBeanDao.Properties.SerialNo.eq(serialNo))
//                    .unique();
//            if (homepage != null) {
//                homepage.setIsHomePage(String.valueOf(isHomepage));
//                mSourceHomepageBeanDao.save(homepage);
//            }
//        }
//    }
//
//    public void changeFromDevice(String serialNo,String deviceSerialNo) {
//        if (!TextUtils.isEmpty(serialNo)) {
//            SourceHomepageBean homepage = mSourceHomepageBeanDao.queryBuilder()
//                    .where(SourceHomepageBeanDao.Properties.SerialNo.eq(serialNo))
//                    .unique();
//            if (homepage != null) {
//                homepage.setDeviceSerialNo(deviceSerialNo);
//                mSourceHomepageBeanDao.save(homepage);
//            }
//        }
//    }
}
